package org.practice.action;

import org.practice.model.User;
import org.practice.service.UserService;
import org.practice.util.Constant;
import org.practice.util.ShopServiceFactory;

@SuppressWarnings("serial")
public class RegisterTwoAction extends BaseAction {
	private String verifyCode;
	@Override
	public String execute() throws Exception {
		String code ="";
		int id =-1;
		if(verifyCode.contains("-")){
			code = verifyCode.substring(0, verifyCode.lastIndexOf("-"));
			id = Integer.parseInt(verifyCode.substring(verifyCode.lastIndexOf("-") + 1));
		}
		
		UserService userService = ShopServiceFactory.createUserService();
		User user = userService.verifyUser(id,code);
		if (user == null) {
			this.addActionMessage(getText("error.verify.emailcode"));
			return FAIL;
		}
		user.setVerify(Boolean.TRUE);
		userService.updateUser(user);
		getSession().setAttribute(Constant.SESSION_KEY_USER, user);
		return SUCCESS;
	}
	public String verifyForm(){
		
		return SUCCESS;
	}
	public String getVerifyCode() {
		return verifyCode;
	}
	public void setVerifyCode(String verifyCode) {
		this.verifyCode = verifyCode;
	}
	
}
